package com.xn.app.muying.adpter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.xn.app.muying.R;
import com.xn.app.muying.bean.ToDayBean;

import java.util.ArrayList;

/**
 * 今日知识列表
 * Created by Administrator on 2016/10/10.
 */
public class HomeAdpter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private ArrayList<ToDayBean> data = new ArrayList();
    private ImageLoader imageLoader = null;
    private DisplayImageOptions options = null;
    AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1f);



    public HomeAdpter(Context context, ImageLoader loader,
                      DisplayImageOptions options) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.imageLoader = loader;
        this.options = options;
        //设置动画持续时长
        alphaAnimation.setDuration(1000);
        //设置动画结束之后的状态是否是动画的最终状态，true，表示是保持动画结束时的最终状态
        alphaAnimation.setFillAfter(true);
    }

    public void getData(ArrayList<ToDayBean> data) {
        this.data=data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder = new ViewHolder();
        if (view == null) {
            view = inflater.inflate(R.layout.home_fragment_floor_today, null);
            holder.iv_picture = (ImageView) view.findViewById(R.id.home_fragment_today_picture);
            holder.tv_title = (TextView) view.findViewById(R.id.home_fragment_today_title);
            holder.tv_content = (TextView) view.findViewById(R.id.home_fragment_today_content);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.tv_title.setText(data.get(position).getTitle());
        holder.tv_content.setText(data.get(position).getShortDescription());
        holder.iv_picture.setTag(data.get(position).getPictureUrl());
        //设置图片，如果内存中有，优先从内存中读取
        holder.iv_picture.setImageResource(0);
       if(data.get(position).getPictureUrl()!=null&&!data.get(position).getPictureUrl().equals("null")) {
           imageLoader.displayImage(data.get(position).getPictureUrl(), holder.iv_picture, options);
       }else {
           holder.iv_picture.setImageResource(R.mipmap.load_failed);
       }
        return view;
    }

    class ViewHolder {
        ImageView iv_picture;
        TextView tv_title;
        TextView tv_content;
    }
}
